package rheise.jftpd;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.net.ServerSocket;
import java.net.Socket;

import java.util.Properties;

/**
 * This is the main class for jftpd. It creates the server socket and loops
 * forever accepting user connections and creating ServerPI threads to handle
 * them.
 * <p>
 * To run under unix, type:
 * 
 * <pre>
 * java rheise.jftpd.Server
 * </pre>
 */
public class Server
// implements org.jos.services.common.Server
{
	/**
	 * The version number of jftpd.
	 */
	public static final String VERSION = "Version 0.3";

	/**
	 * The port this server connects to.
	 */
	public static final int SERVER_PORT = 21;

	/**
	 * The default data port.
	 */
	public static final int SERVER_DATA_PORT = 20;

	/**
	 * The port this server is listening on.
	 */
	private int port;

	public static void main(String[] args) throws Exception {

		Server server = new Server();

		// Start the FTP server as a standalone server.
		//
		server.start();
	}

	/**
	 * Create an FTP server to run on the specified port.
	 */
	public Server() throws ParseException {
		this.port = SERVER_PORT;
	}

	/**
	 * Starts the FTP server. This method listens for connections on the FTP
	 * server port (usually port 21), and spawns a new thread to handle each
	 * connection.
	 */
	private void start() throws Exception {
		ServerSocket serverSocket = new ServerSocket(port);
		while (true) {
			Socket clientSocket = serverSocket.accept();
			ServerPI pi = new ServerPI(clientSocket);
			new Thread(pi).start();
		}
	}

	/**
	 * Handle a client connection. This may be called directly by a superserver
	 * instead of start().
	 * 
	 * @param socket
	 *            the client socket.
	 */
	public void service(Socket socket) throws Exception {
		ServerPI pi = new ServerPI(socket);
		pi.run();
	}

	/**
	 * Configures the ftp server from the configuration properties.
	 */
	// private void configure(Properties properties)
	// throws ParseException
	// {
	// String priorityStr = properties.getProperty("log.priority");
	// if (priorityStr == null)
	// throw new ParseException(ParseException.PROPERTY_UNDEFINED,
	// "log.priority", null);
	// String priorityNames[] = Logger.priorityNames;
	// int priority = Logger.LOG_OFF;
	// for (int i = 0; i < priorityNames.length; i++)
	// {
	// if (priorityNames[i].equals(priorityStr))
	// {
	// priority = i;
	// break;
	// }
	// }
	//
	// Logger.setPriority(priority);
	//
	// String output = properties.getProperty("log.output");
	// OutputStream outputStream = null;
	// if (output == null || output.equals("stderr"))
	// {
	// outputStream = System.err;
	// }
	// else
	// {
	// try
	// {
	// outputStream = new FileOutputStream(output, true);
	// }
	// catch (IOException e)
	// {
	// throw new ParseException(ParseException.PROPERTY_INVALID, "log.output",
	// output);
	// }
	// }
	// Logger.setWriter(new PrintWriter(new OutputStreamWriter(outputStream),
	// true));
	// }
}